﻿package com.badibadi.adapter;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.tsz.afinal.FinalBitmap;

import com.badibadi.mytools.DisplayUtil;
import com.badibadi.mytools.ImageLoader;
import com.badibadi.uniclubber.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class UpPictures1SDcardFileAdapter extends BaseAdapter {

	private DisplayUtil displayUtil;
	private ImageLoader imageLoader;
	private final String TAG = "aaa";
	private Context context;
	/**
	 * 子文件名
	 */
	private String[] items;
	/**
	 * 记录当前路径
	 */
	private String dangqianpath;
	private LayoutInflater layoutInflater;
	private float dip50_px;

	
	
	private FinalBitmap finalBitmap;
	public UpPictures1SDcardFileAdapter(Context ct, String dangqianPath,
			String[] items) {
		context = ct;
		finalBitmap=FinalBitmap.create(context);
		this.dangqianpath = dangqianPath;
		this.items = items;
//		   
		displayUtil = new DisplayUtil();
		dip50_px = DisplayUtil.dip2px(context, 50);
		imageLoader = ImageLoader.getInstance();
		finalBitmap=FinalBitmap.create(context);
		
		
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
//		   
		return items.length;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
//		   
		return (dangqianpath + "/" + items[position]);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
//		   
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder viewHolder = new ViewHolder();
		if(convertView==null)
		{
			layoutInflater = LayoutInflater.from(context);
			convertView = layoutInflater.inflate(
					R.layout.up_pictures1_sdcard_manager_import_layout, null);
			
			
			viewHolder.fileName = (TextView) convertView
					.findViewById(R.id.sdcard_file_filename);
			viewHolder.fileNameNumber = (TextView) convertView
					.findViewById(R.id.sdcard_file_filename_number);
			viewHolder.Img = (ImageView) convertView
					.findViewById(R.id.sdcard_file_ImgSet);
			convertView.setTag(viewHolder);
		}else
		  {
			viewHolder = (ViewHolder)convertView.getTag();
		  }
		
		viewHolder.fileName.setText(items[position]);
		

		// 接下来要获取子目录的第一张图片
		// File fchild=new File(dangqianpath, items[position]);
		// String[] li=null;
		// if(fchild.isDirectory()){
		// li = fchild.list();
		// }
		// File file1=new File(dangqianpath,items[position]);
		// String[] li=null;
		// if(file1.isDirectory())
		// {
		// li = file1.list();
		// viewHolder.fileNameNumber.setText("("+li.length+")");
		// }
		
		
		
		asyncImageLoad(viewHolder.Img, viewHolder.fileNameNumber, dangqianpath
				+ "/" + items[position]);
		return convertView;
	}

	/**
	 * 重要
	 * 
	 * @param imageView
	 * @param path
	 */
	private void asyncImageLoad(ImageView imageView, TextView textView,
			String dangqianzilujing) {
		AsyncImageTask asyncImageTask = new AsyncImageTask(imageView, textView);
		asyncImageTask.execute(dangqianzilujing);
	}

	// AsyncTask<Params, Progress, Result>
	/**
	 * 重要
	 * 
	 * @author Administrator 第一个String传入doInBackground 第二个是进度
	 *         第三个是doInBackground返回给onPostExecute
	 */
	private final class AsyncImageTask extends
			AsyncTask<String, Integer, Bitmap> {
		private ImageView imageView;
		private TextView textView;
		private String dangqianlujing;
		private int wenjiannumber = 0;

		public AsyncImageTask(ImageView imageView, TextView textView) {
			// TODO Auto-generated constructor stub
			this.imageView = imageView;
			this.textView = textView;
		}

		@Override
		protected Bitmap doInBackground(String... params) {// 子线程中执行
			// TODO Auto-generated method stub
			dangqianlujing = params[0];
//			   
			File file1 = new File(dangqianlujing);

			String[] li = null;
			if (file1.isDirectory()) {
				li = file1.list();
			}
			if (li != null) {
				for (int i = 0; i < li.length; i++) {
					if (li[i].lastIndexOf(".jpg") != -1
							|| li[i].lastIndexOf(".png") != -1
							|| li[i].lastIndexOf(".jif") != -1) {
						// File ff=new File(dangqianlujing,li[i]);
						//
						// Bitmap bmp=null;
						// try {
						// FileInputStream fis=new FileInputStream(ff);
						// BufferedInputStream bis=new BufferedInputStream(fis);
						// bmp=BitmapFactory.decodeStream(bis);
						// //   
						// bis.close();
						// fis.close();
						// } catch (Exception e) {
						// // TODO Auto-generated catch block
						//    
						// }
						//
						// // Uri uri=Uri.fromFile(ff);

						String pathName = dangqianlujing + "/" + li[i];
						Bitmap bmp = imageLoader
								.decodeSampledBitmapFromResource(pathName,
										(int) dip50_px);
						wenjiannumber = li.length;
						return bmp;
					}

				}

			}
			return null;
		}

		@Override
		protected void onPostExecute(Bitmap result) {// 运行在主线程
			// TODO Auto-generated method stub
			if (result != null && imageView != null) {
				
//				finalBitmap.display(imageView, uri);
				imageView.setImageBitmap(result);

			}
			if (wenjiannumber != 0 && textView != null) {
				textView.setText("(" + wenjiannumber + ")");
			}

		}
	}

	static class ViewHolder {
		public ImageView Img;
		public TextView fileName;
		public TextView fileNameNumber;
	}

	public long getlist(File f) {// 递归求取目录文件个数
		long size = 0;
		File flist[] = f.listFiles();
		size = flist.length;
		for (int i = 0; i < flist.length; i++) {
			if (flist[i].isDirectory()) {
				size = size + getlist(flist[i]);
				size--;
			}
		}
		return size;
	}

	public String getDangqianpath() {
		return dangqianpath;
	}

	public void setDangqianpath(String dangqianpath) {
		this.dangqianpath = dangqianpath;
	}

	public String[] getItems() {
		return items;
	}

	public void setItems(String[] items) {
		this.items = items;
	}

}
